<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: samples/dnn/object_detection.cpp</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">samples/dnn/object_detection.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>Check <a class="el" href="../../da/d9d/tutorial_dnn_yolo.html">the corresponding tutorial</a> for more details</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;fstream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;sstream&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d9/d8c/dnn_8hpp.html">opencv2/dnn.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&gt;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#ifdef CV_CXX11</span></div><div class="line"><span class="preprocessor">#include &lt;mutex&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;thread&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;queue&gt;</span></div><div class="line"><span class="preprocessor">#endif</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &quot;common.hpp&quot;</span></div><div class="line"></div><div class="line">std::string keys =</div><div class="line">    <span class="stringliteral">&quot;{ help  h     | | Print help message. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{ @alias      | | An alias name of model to extract preprocessing parameters from models.yml file. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{ zoo         | models.yml | An optional path to file with preprocessing parameters }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{ device      |  0 | camera device number. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{ input i     | | Path to input image or video file. Skip this argument to capture frames from a camera. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{ framework f | | Optional name of an origin framework of the model. Detect it automatically if it does not set. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{ classes     | | Optional path to a text file with names of classes to label detected objects. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{ thr         | .5 | Confidence threshold. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{ nms         | .4 | Non-maximum suppression threshold. }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{ backend     |  0 | Choose one of computation backends: &quot;</span></div><div class="line">                         <span class="stringliteral">&quot;0: automatically (by default), &quot;</span></div><div class="line">                         <span class="stringliteral">&quot;1: Halide language (http://halide-lang.org/), &quot;</span></div><div class="line">                         <span class="stringliteral">&quot;2: Intel&#39;s Deep Learning Inference Engine (https://software.intel.com/openvino-toolkit), &quot;</span></div><div class="line">                         <span class="stringliteral">&quot;3: OpenCV implementation }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{ target      | 0 | Choose one of target computation devices: &quot;</span></div><div class="line">                         <span class="stringliteral">&quot;0: CPU target (by default), &quot;</span></div><div class="line">                         <span class="stringliteral">&quot;1: OpenCL, &quot;</span></div><div class="line">                         <span class="stringliteral">&quot;2: OpenCL fp16 (half-float precision), &quot;</span></div><div class="line">                         <span class="stringliteral">&quot;3: VPU }&quot;</span></div><div class="line">    <span class="stringliteral">&quot;{ async       | 0 | Number of asynchronous forwards at the same time. &quot;</span></div><div class="line">                        <span class="stringliteral">&quot;Choose 0 for synchronous mode }&quot;</span>;</div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"><span class="keyword">using namespace </span>dnn;</div><div class="line"></div><div class="line"><span class="keywordtype">float</span> confThreshold, nmsThreshold;</div><div class="line">std::vector&lt;std::string&gt; classes;</div><div class="line"></div><div class="line"><span class="keyword">inline</span> <span class="keywordtype">void</span> preprocess(<span class="keyword">const</span> <a name="_a0"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; frame, Net&amp; net, <a name="_a1"></a><a class="code" href="../../d6/d50/classcv_1_1Size__.html">Size</a> inpSize, <span class="keywordtype">float</span> <a name="a2"></a><a class="code" href="../../d6/d84/namespacecv_1_1quality_1_1quality__utils.html#ae55d1c89ff5761730174745401162743">scale</a>,</div><div class="line">                       <span class="keyword">const</span> <a name="_a3"></a><a class="code" href="../../d1/da0/classcv_1_1Scalar__.html">Scalar</a>&amp; <a name="a4"></a><a class="code" href="../../d2/de8/group__core__array.html#ga191389f8a0e58180bb13a727782cd461">mean</a>, <span class="keywordtype">bool</span> swapRB);</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> <a name="a5"></a><a class="code" href="../../d2/daf/namespacecv_1_1detail.html#a90788819ae75ed8cc94331159859d133">postprocess</a>(<a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; frame, <span class="keyword">const</span> std::vector&lt;Mat&gt;&amp; out, Net&amp; net, <span class="keywordtype">int</span> <a name="a6"></a><a class="code" href="../../dc/d1c/group__gapi__std__backends.html#ga0ea1033f48e85753bcd35218f7384ea7">backend</a>);</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> drawPred(<span class="keywordtype">int</span> classId, <span class="keywordtype">float</span> conf, <span class="keywordtype">int</span> left, <span class="keywordtype">int</span> top, <span class="keywordtype">int</span> right, <span class="keywordtype">int</span> bottom, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; frame);</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> callback(<span class="keywordtype">int</span> pos, <span class="keywordtype">void</span>* userdata);</div><div class="line"></div><div class="line"><span class="preprocessor">#ifdef CV_CXX11</span></div><div class="line"><span class="keyword">template</span> &lt;<span class="keyword">typename</span> T&gt;</div><div class="line"><span class="keyword">class </span>QueueFPS : <span class="keyword">public</span> std::queue&lt;T&gt;</div><div class="line">{</div><div class="line"><span class="keyword">public</span>:</div><div class="line">    QueueFPS() : counter(0) {}</div><div class="line"></div><div class="line">    <span class="keywordtype">void</span> push(<span class="keyword">const</span> T&amp; entry)</div><div class="line">    {</div><div class="line">        std::lock_guard&lt;std::mutex&gt; lock(mutex);</div><div class="line"></div><div class="line">        std::queue&lt;T&gt;::push(entry);</div><div class="line">        counter += 1;</div><div class="line">        <span class="keywordflow">if</span> (counter == 1)</div><div class="line">        {</div><div class="line">            <span class="comment">// Start counting from a second frame (warmup).</span></div><div class="line">            tm.reset();</div><div class="line">            tm.start();</div><div class="line">        }</div><div class="line">    }</div><div class="line"></div><div class="line">    T <span class="keyword">get</span>()</div><div class="line">    {</div><div class="line">        std::lock_guard&lt;std::mutex&gt; lock(mutex);</div><div class="line">        T entry = this-&gt;front();</div><div class="line">        this-&gt;pop();</div><div class="line">        <span class="keywordflow">return</span> entry;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordtype">float</span> getFPS()</div><div class="line">    {</div><div class="line">        tm.stop();</div><div class="line">        <span class="keywordtype">double</span> fps = counter / tm.getTimeSec();</div><div class="line">        tm.start();</div><div class="line">        <span class="keywordflow">return</span> <span class="keyword">static_cast&lt;</span><span class="keywordtype">float</span><span class="keyword">&gt;</span>(fps);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordtype">void</span> clear()</div><div class="line">    {</div><div class="line">        std::lock_guard&lt;std::mutex&gt; lock(mutex);</div><div class="line">        <span class="keywordflow">while</span> (!this-&gt;empty())</div><div class="line">            this-&gt;pop();</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> counter;</div><div class="line"></div><div class="line"><span class="keyword">private</span>:</div><div class="line">    <a name="_a7"></a><a class="code" href="../../d9/d6f/classcv_1_1TickMeter.html">TickMeter</a> tm;</div><div class="line">    std::mutex mutex;</div><div class="line">};</div><div class="line"><span class="preprocessor">#endif  // CV_CXX11</span></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv)</div><div class="line">{</div><div class="line">    <a name="_a8"></a><a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html">CommandLineParser</a> parser(argc, argv, keys);</div><div class="line"></div><div class="line">    <span class="keyword">const</span> std::string modelName = parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;@alias&quot;</span>);</div><div class="line">    <span class="keyword">const</span> std::string zooFile = parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;zoo&quot;</span>);</div><div class="line"></div><div class="line">    keys += genPreprocArguments(modelName, zooFile);</div><div class="line"></div><div class="line">    parser = <a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html">CommandLineParser</a>(argc, argv, keys);</div><div class="line">    parser.about(<span class="stringliteral">&quot;Use this script to run object detection deep learning networks using OpenCV.&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span> (argc == 1 || parser.has(<span class="stringliteral">&quot;help&quot;</span>))</div><div class="line">    {</div><div class="line">        parser.printMessage();</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line"></div><div class="line">    confThreshold = parser.get&lt;<span class="keywordtype">float</span>&gt;(<span class="stringliteral">&quot;thr&quot;</span>);</div><div class="line">    nmsThreshold = parser.get&lt;<span class="keywordtype">float</span>&gt;(<span class="stringliteral">&quot;nms&quot;</span>);</div><div class="line">    <span class="keywordtype">float</span> <a class="code" href="../../d6/d84/namespacecv_1_1quality_1_1quality__utils.html#ae55d1c89ff5761730174745401162743">scale</a> = parser.get&lt;<span class="keywordtype">float</span>&gt;(<span class="stringliteral">&quot;scale&quot;</span>);</div><div class="line">    <a class="code" href="../../d1/da0/classcv_1_1Scalar__.html">Scalar</a> <a class="code" href="../../d2/de8/group__core__array.html#ga191389f8a0e58180bb13a727782cd461">mean</a> = parser.get&lt;<a class="code" href="../../d1/da0/classcv_1_1Scalar__.html">Scalar</a>&gt;(<span class="stringliteral">&quot;mean&quot;</span>);</div><div class="line">    <span class="keywordtype">bool</span> swapRB = parser.get&lt;<span class="keywordtype">bool</span>&gt;(<span class="stringliteral">&quot;rgb&quot;</span>);</div><div class="line">    <span class="keywordtype">int</span> inpWidth = parser.get&lt;<span class="keywordtype">int</span>&gt;(<span class="stringliteral">&quot;width&quot;</span>);</div><div class="line">    <span class="keywordtype">int</span> inpHeight = parser.get&lt;<span class="keywordtype">int</span>&gt;(<span class="stringliteral">&quot;height&quot;</span>);</div><div class="line">    <span class="keywordtype">size_t</span> asyncNumReq = parser.get&lt;<span class="keywordtype">int</span>&gt;(<span class="stringliteral">&quot;async&quot;</span>);</div><div class="line">    <a name="a9"></a><a class="code" href="../../db/de0/group__core__utils.html#gaf62bcd90f70e275191ab95136d85906b">CV_Assert</a>(parser.has(<span class="stringliteral">&quot;model&quot;</span>));</div><div class="line">    std::string modelPath = <a name="a10"></a><a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">findFile</a>(parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;model&quot;</span>));</div><div class="line">    std::string configPath = <a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">findFile</a>(parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;config&quot;</span>));</div><div class="line"></div><div class="line">    <span class="comment">// Open file with classes names.</span></div><div class="line">    <span class="keywordflow">if</span> (parser.has(<span class="stringliteral">&quot;classes&quot;</span>))</div><div class="line">    {</div><div class="line">        std::string file = parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;classes&quot;</span>);</div><div class="line">        std::ifstream ifs(file.c_str());</div><div class="line">        <span class="keywordflow">if</span> (!ifs.is_open())</div><div class="line">            <a name="a11"></a><a class="code" href="../../db/de0/group__core__utils.html#ga5b48c333c777666e076bd7052799f891">CV_Error</a>(<a name="a12"></a><a class="code" href="../../d1/d0d/namespacecv_1_1Error.html#a759fa1af92f7aa7377c76ffb142abccaacf93e97abba2e7defa74fe5b99e122ac">Error::StsError</a>, <span class="stringliteral">&quot;File &quot;</span> + file + <span class="stringliteral">&quot; not found&quot;</span>);</div><div class="line">        std::string <a name="a13"></a><a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga7078a9fae8c7e7d13d24dac2520ae4a2">line</a>;</div><div class="line">        <span class="keywordflow">while</span> (std::getline(ifs, line))</div><div class="line">        {</div><div class="line">            classes.push_back(line);</div><div class="line">        }</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="comment">// Load a model.</span></div><div class="line">    Net net = <a name="a14"></a><a class="code" href="../../d6/d0f/group__dnn.html#ga3b34fe7a29494a6a4295c169a7d32422">readNet</a>(modelPath, configPath, parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;framework&quot;</span>));</div><div class="line">    <span class="keywordtype">int</span> <a class="code" href="../../dc/d1c/group__gapi__std__backends.html#ga0ea1033f48e85753bcd35218f7384ea7">backend</a> = parser.get&lt;<span class="keywordtype">int</span>&gt;(<span class="stringliteral">&quot;backend&quot;</span>);</div><div class="line">    net.setPreferableBackend(backend);</div><div class="line">    net.setPreferableTarget(parser.get&lt;<span class="keywordtype">int</span>&gt;(<span class="stringliteral">&quot;target&quot;</span>));</div><div class="line">    std::vector&lt;String&gt; outNames = net.getUnconnectedOutLayersNames();</div><div class="line"></div><div class="line">    <span class="comment">// Create a window</span></div><div class="line">    <span class="keyword">static</span> <span class="keyword">const</span> std::string kWinName = <span class="stringliteral">&quot;Deep learning object detection in OpenCV&quot;</span>;</div><div class="line">    <a name="a15"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>(kWinName, <a name="a16"></a><a class="code" href="../../d0/d90/group__highgui__window__flags.html#ggabf7d2c5625bc59ac130287f925557ac3a29e45c5af696f73ce5e153601e5ca0f1">WINDOW_NORMAL</a>);</div><div class="line">    <span class="keywordtype">int</span> initialConf = (int)(confThreshold * 100);</div><div class="line">    <a name="a17"></a><a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>(<span class="stringliteral">&quot;Confidence threshold, %&quot;</span>, kWinName, &amp;initialConf, 99, callback);</div><div class="line"></div><div class="line">    <span class="comment">// Open a video file or an image file or a camera stream.</span></div><div class="line">    <a name="_a18"></a><a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">VideoCapture</a> cap;</div><div class="line">    <span class="keywordflow">if</span> (parser.has(<span class="stringliteral">&quot;input&quot;</span>))</div><div class="line">        cap.<a name="a19"></a><a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#a614a1702e15f42ede5100014ce7f48ed">open</a>(parser.get&lt;<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>&gt;(<span class="stringliteral">&quot;input&quot;</span>));</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">        cap.<a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#a614a1702e15f42ede5100014ce7f48ed">open</a>(parser.get&lt;<span class="keywordtype">int</span>&gt;(<span class="stringliteral">&quot;device&quot;</span>));</div><div class="line"></div><div class="line"><span class="preprocessor">#ifdef CV_CXX11</span></div><div class="line">    <span class="keywordtype">bool</span> process = <span class="keyword">true</span>;</div><div class="line"></div><div class="line">    <span class="comment">// Frames capturing thread</span></div><div class="line">    QueueFPS&lt;Mat&gt; framesQueue;</div><div class="line">    std::thread framesThread([&amp;](){</div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> frame;</div><div class="line">        <span class="keywordflow">while</span> (process)</div><div class="line">        {</div><div class="line">            cap &gt;&gt; frame;</div><div class="line">            <span class="keywordflow">if</span> (!frame.empty())</div><div class="line">                framesQueue.push(frame.clone());</div><div class="line">            <span class="keywordflow">else</span></div><div class="line">                <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line">    });</div><div class="line"></div><div class="line">    <span class="comment">// Frames processing thread</span></div><div class="line">    QueueFPS&lt;Mat&gt; processedFramesQueue;</div><div class="line">    QueueFPS&lt;std::vector&lt;Mat&gt; &gt; predictionsQueue;</div><div class="line">    std::thread processingThread([&amp;](){</div><div class="line">        std::queue&lt;AsyncArray&gt; futureOutputs;</div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> blob;</div><div class="line">        <span class="keywordflow">while</span> (process)</div><div class="line">        {</div><div class="line">            <span class="comment">// Get a next frame</span></div><div class="line">            <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> frame;</div><div class="line">            {</div><div class="line">                <span class="keywordflow">if</span> (!framesQueue.empty())</div><div class="line">                {</div><div class="line">                    frame = framesQueue.get();</div><div class="line">                    <span class="keywordflow">if</span> (asyncNumReq)</div><div class="line">                    {</div><div class="line">                        <span class="keywordflow">if</span> (futureOutputs.size() == asyncNumReq)</div><div class="line">                            frame = <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>();</div><div class="line">                    }</div><div class="line">                    <span class="keywordflow">else</span></div><div class="line">                        framesQueue.clear();  <span class="comment">// Skip the rest of frames</span></div><div class="line">                }</div><div class="line">            }</div><div class="line"></div><div class="line">            <span class="comment">// Process the frame</span></div><div class="line">            <span class="keywordflow">if</span> (!frame.<a name="a20"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>())</div><div class="line">            {</div><div class="line">                preprocess(frame, net, <a name="a21"></a><a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(inpWidth, inpHeight), scale, mean, swapRB);</div><div class="line">                processedFramesQueue.push(frame);</div><div class="line"></div><div class="line">                <span class="keywordflow">if</span> (asyncNumReq)</div><div class="line">                {</div><div class="line">                    futureOutputs.push(net.forwardAsync());</div><div class="line">                }</div><div class="line">                <span class="keywordflow">else</span></div><div class="line">                {</div><div class="line">                    std::vector&lt;Mat&gt; outs;</div><div class="line">                    net.forward(outs, outNames);</div><div class="line">                    predictionsQueue.push(outs);</div><div class="line">                }</div><div class="line">            }</div><div class="line"></div><div class="line">            <span class="keywordflow">while</span> (!futureOutputs.empty() &amp;&amp;</div><div class="line">                   futureOutputs.front().wait_for(std::chrono::seconds(0)))</div><div class="line">            {</div><div class="line">                <a name="_a22"></a><a class="code" href="../../d4/d20/classcv_1_1AsyncArray.html">AsyncArray</a> async_out = futureOutputs.front();</div><div class="line">                futureOutputs.pop();</div><div class="line">                <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> out;</div><div class="line">                async_out.<a name="a23"></a><a class="code" href="../../d4/d20/classcv_1_1AsyncArray.html#aca6c8dda8353ab799a815b15d55668d4">get</a>(out);</div><div class="line">                predictionsQueue.push({out});</div><div class="line">            }</div><div class="line">        }</div><div class="line">    });</div><div class="line"></div><div class="line">    <span class="comment">// Postprocessing and rendering loop</span></div><div class="line">    <span class="keywordflow">while</span> (<a name="a24"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(1) &lt; 0)</div><div class="line">    {</div><div class="line">        <span class="keywordflow">if</span> (predictionsQueue.empty())</div><div class="line">            <span class="keywordflow">continue</span>;</div><div class="line"></div><div class="line">        std::vector&lt;Mat&gt; outs = predictionsQueue.get();</div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> frame = processedFramesQueue.get();</div><div class="line"></div><div class="line">        <a class="code" href="../../d2/daf/namespacecv_1_1detail.html#a90788819ae75ed8cc94331159859d133">postprocess</a>(frame, outs, net, backend);</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> (predictionsQueue.counter &gt; 1)</div><div class="line">        {</div><div class="line">            std::string label = format(<span class="stringliteral">&quot;Camera: %.2f FPS&quot;</span>, framesQueue.getFPS());</div><div class="line">            <a name="a25"></a><a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga5126f47f883d730f633d74f07456c576">putText</a>(frame, label, <a name="a26"></a><a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(0, 15), <a name="a27"></a><a class="code" href="../../d6/d6e/group__imgproc__draw.html#gga0f9314ea6e35f99bb23f29567fc16e11afff8b973668df2e4028dddc5274310c9">FONT_HERSHEY_SIMPLEX</a>, 0.5, <a name="a28"></a><a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0, 255, 0));</div><div class="line"></div><div class="line">            label = format(<span class="stringliteral">&quot;Network: %.2f FPS&quot;</span>, predictionsQueue.getFPS());</div><div class="line">            <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga5126f47f883d730f633d74f07456c576">putText</a>(frame, label, <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(0, 30), <a class="code" href="../../d6/d6e/group__imgproc__draw.html#gga0f9314ea6e35f99bb23f29567fc16e11afff8b973668df2e4028dddc5274310c9">FONT_HERSHEY_SIMPLEX</a>, 0.5, <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0, 255, 0));</div><div class="line"></div><div class="line">            label = format(<span class="stringliteral">&quot;Skipped frames: %d&quot;</span>, framesQueue.counter - predictionsQueue.counter);</div><div class="line">            <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga5126f47f883d730f633d74f07456c576">putText</a>(frame, label, <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(0, 45), <a class="code" href="../../d6/d6e/group__imgproc__draw.html#gga0f9314ea6e35f99bb23f29567fc16e11afff8b973668df2e4028dddc5274310c9">FONT_HERSHEY_SIMPLEX</a>, 0.5, <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0, 255, 0));</div><div class="line">        }</div><div class="line">        <a name="a29"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(kWinName, frame);</div><div class="line">    }</div><div class="line"></div><div class="line">    process = <span class="keyword">false</span>;</div><div class="line">    framesThread.join();</div><div class="line">    processingThread.join();</div><div class="line"></div><div class="line"><span class="preprocessor">#else  // CV_CXX11</span></div><div class="line">    <span class="keywordflow">if</span> (asyncNumReq)</div><div class="line">        <a class="code" href="../../db/de0/group__core__utils.html#ga5b48c333c777666e076bd7052799f891">CV_Error</a>(<a name="a30"></a><a class="code" href="../../d1/d0d/namespacecv_1_1Error.html#a759fa1af92f7aa7377c76ffb142abccaab5d0063314f64d350a93d091b542d465">Error::StsNotImplemented</a>, <span class="stringliteral">&quot;Asynchronous forward is supported only with Inference Engine backend.&quot;</span>);</div><div class="line"></div><div class="line">    <span class="comment">// Process frames.</span></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> frame, blob;</div><div class="line">    <span class="keywordflow">while</span> (<a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(1) &lt; 0)</div><div class="line">    {</div><div class="line">        cap &gt;&gt; frame;</div><div class="line">        <span class="keywordflow">if</span> (frame.empty())</div><div class="line">        {</div><div class="line">            <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>();</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line"></div><div class="line">        preprocess(frame, net, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(inpWidth, inpHeight), scale, mean, swapRB);</div><div class="line"></div><div class="line">        std::vector&lt;Mat&gt; outs;</div><div class="line">        net.forward(outs, outNames);</div><div class="line"></div><div class="line">        <a class="code" href="../../d2/daf/namespacecv_1_1detail.html#a90788819ae75ed8cc94331159859d133">postprocess</a>(frame, outs, net, backend);</div><div class="line"></div><div class="line">        <span class="comment">// Put efficiency information.</span></div><div class="line">        std::vector&lt;double&gt; layersTimes;</div><div class="line">        <span class="keywordtype">double</span> freq = <a name="a31"></a><a class="code" href="../../db/de0/group__core__utils.html#ga705441a9ef01f47acdc55d87fbe5090c">getTickFrequency</a>() / 1000;</div><div class="line">        <span class="keywordtype">double</span> t = net.getPerfProfile(layersTimes) / freq;</div><div class="line">        std::string label = format(<span class="stringliteral">&quot;Inference time: %.2f ms&quot;</span>, t);</div><div class="line">        <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga5126f47f883d730f633d74f07456c576">putText</a>(frame, label, <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(0, 15), <a class="code" href="../../d6/d6e/group__imgproc__draw.html#gga0f9314ea6e35f99bb23f29567fc16e11afff8b973668df2e4028dddc5274310c9">FONT_HERSHEY_SIMPLEX</a>, 0.5, <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0, 255, 0));</div><div class="line"></div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(kWinName, frame);</div><div class="line">    }</div><div class="line"><span class="preprocessor">#endif  // CV_CXX11</span></div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">inline</span> <span class="keywordtype">void</span> preprocess(<span class="keyword">const</span> <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; frame, Net&amp; net, <a class="code" href="../../d6/d50/classcv_1_1Size__.html">Size</a> inpSize, <span class="keywordtype">float</span> scale,</div><div class="line">                       <span class="keyword">const</span> <a class="code" href="../../d1/da0/classcv_1_1Scalar__.html">Scalar</a>&amp; mean, <span class="keywordtype">bool</span> swapRB)</div><div class="line">{</div><div class="line">    <span class="keyword">static</span> <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> blob;</div><div class="line">    <span class="comment">// Create a 4D blob from a frame.</span></div><div class="line">    <span class="keywordflow">if</span> (inpSize.<a name="a32"></a><a class="code" href="../../d6/d50/classcv_1_1Size__.html#abfe0367b32c407ddccf5ddf92667c73d">width</a> &lt;= 0) inpSize.<a class="code" href="../../d6/d50/classcv_1_1Size__.html#abfe0367b32c407ddccf5ddf92667c73d">width</a> = frame.<a name="a33"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>;</div><div class="line">    <span class="keywordflow">if</span> (inpSize.<a name="a34"></a><a class="code" href="../../d6/d50/classcv_1_1Size__.html#a1d289dce6b5d8006a54f3ee0259fc545">height</a> &lt;= 0) inpSize.<a class="code" href="../../d6/d50/classcv_1_1Size__.html#a1d289dce6b5d8006a54f3ee0259fc545">height</a> = frame.<a name="a35"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>;</div><div class="line">    <a name="a36"></a><a class="code" href="../../d6/d0f/group__dnn.html#ga29f34df9376379a603acd8df581ac8d7">blobFromImage</a>(frame, blob, 1.0, inpSize, <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(), swapRB, <span class="keyword">false</span>, <a name="a37"></a><a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga32b18d904ee2b1731a9416a8eef67d06">CV_8U</a>);</div><div class="line"></div><div class="line">    <span class="comment">// Run a model.</span></div><div class="line">    net.setInput(blob, <span class="stringliteral">&quot;&quot;</span>, scale, mean);</div><div class="line">    <span class="keywordflow">if</span> (net.getLayer(0)-&gt;outputNameToIndex(<span class="stringliteral">&quot;im_info&quot;</span>) != -1)  <span class="comment">// Faster-RCNN or R-FCN</span></div><div class="line">    {</div><div class="line">        <a name="a38"></a><a class="code" href="../../da/d54/group__imgproc__transform.html#ga47a974309e9102f5f08231edc7e7529d">resize</a>(frame, frame, inpSize);</div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> imInfo = (<a name="_a39"></a><a class="code" href="../../df/dfc/classcv_1_1Mat__.html">Mat_&lt;float&gt;</a>(1, 3) &lt;&lt; inpSize.<a class="code" href="../../d6/d50/classcv_1_1Size__.html#a1d289dce6b5d8006a54f3ee0259fc545">height</a>, inpSize.<a class="code" href="../../d6/d50/classcv_1_1Size__.html#abfe0367b32c407ddccf5ddf92667c73d">width</a>, 1.6f);</div><div class="line">        net.setInput(imInfo, <span class="stringliteral">&quot;im_info&quot;</span>);</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> <a class="code" href="../../d2/daf/namespacecv_1_1detail.html#a90788819ae75ed8cc94331159859d133">postprocess</a>(<a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; frame, <span class="keyword">const</span> std::vector&lt;Mat&gt;&amp; outs, Net&amp; net, <span class="keywordtype">int</span> backend)</div><div class="line">{</div><div class="line">    <span class="keyword">static</span> std::vector&lt;int&gt; outLayers = net.getUnconnectedOutLayers();</div><div class="line">    <span class="keyword">static</span> std::string outLayerType = net.getLayer(outLayers[0])-&gt;type;</div><div class="line"></div><div class="line">    std::vector&lt;int&gt; classIds;</div><div class="line">    std::vector&lt;float&gt; confidences;</div><div class="line">    std::vector&lt;Rect&gt; boxes;</div><div class="line">    <span class="keywordflow">if</span> (outLayerType == <span class="stringliteral">&quot;DetectionOutput&quot;</span>)</div><div class="line">    {</div><div class="line">        <span class="comment">// Network produces output blob with a shape 1x1xNx7 where N is a number of</span></div><div class="line">        <span class="comment">// detections and an every detection is a vector of values</span></div><div class="line">        <span class="comment">// [batchId, classId, confidence, left, top, right, bottom]</span></div><div class="line">        <a class="code" href="../../db/de0/group__core__utils.html#gaf62bcd90f70e275191ab95136d85906b">CV_Assert</a>(outs.size() &gt; 0);</div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> k = 0; k &lt; outs.size(); k++)</div><div class="line">        {</div><div class="line">            <span class="keywordtype">float</span>* data = (<span class="keywordtype">float</span>*)outs[k].data;</div><div class="line">            <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i &lt; outs[k].total(); i += 7)</div><div class="line">            {</div><div class="line">                <span class="keywordtype">float</span> confidence = data[i + 2];</div><div class="line">                <span class="keywordflow">if</span> (confidence &gt; confThreshold)</div><div class="line">                {</div><div class="line">                    <span class="keywordtype">int</span> left   = (int)data[i + 3];</div><div class="line">                    <span class="keywordtype">int</span> top    = (int)data[i + 4];</div><div class="line">                    <span class="keywordtype">int</span> right  = (int)data[i + 5];</div><div class="line">                    <span class="keywordtype">int</span> bottom = (int)data[i + 6];</div><div class="line">                    <span class="keywordtype">int</span> width  = right - left + 1;</div><div class="line">                    <span class="keywordtype">int</span> height = bottom - top + 1;</div><div class="line">                    <span class="keywordflow">if</span> (width &lt;= 2 || height &lt;= 2)</div><div class="line">                    {</div><div class="line">                        left   = (int)(data[i + 3] * frame.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>);</div><div class="line">                        top    = (int)(data[i + 4] * frame.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>);</div><div class="line">                        right  = (int)(data[i + 5] * frame.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>);</div><div class="line">                        bottom = (int)(data[i + 6] * frame.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>);</div><div class="line">                        width  = right - left + 1;</div><div class="line">                        height = bottom - top + 1;</div><div class="line">                    }</div><div class="line">                    classIds.push_back((<span class="keywordtype">int</span>)(data[i + 1]) - 1);  <span class="comment">// Skip 0th background class id.</span></div><div class="line">                    boxes.push_back(<a name="a40"></a><a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a>(left, top, width, height));</div><div class="line">                    confidences.push_back(confidence);</div><div class="line">                }</div><div class="line">            }</div><div class="line">        }</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span> <span class="keywordflow">if</span> (outLayerType == <span class="stringliteral">&quot;Region&quot;</span>)</div><div class="line">    {</div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i &lt; outs.size(); ++i)</div><div class="line">        {</div><div class="line">            <span class="comment">// Network produces output blob with a shape NxC where N is a number of</span></div><div class="line">            <span class="comment">// detected objects and C is a number of classes + 4 where the first 4</span></div><div class="line">            <span class="comment">// numbers are [center_x, center_y, width, height]</span></div><div class="line">            <span class="keywordtype">float</span>* data = (<span class="keywordtype">float</span>*)outs[i].data;</div><div class="line">            <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; outs[i].rows; ++j, data += outs[i].cols)</div><div class="line">            {</div><div class="line">                <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> scores = outs[i].row(j).colRange(5, outs[i].cols);</div><div class="line">                <a name="_a41"></a><a class="code" href="../../db/d4e/classcv_1_1Point__.html">Point</a> classIdPoint;</div><div class="line">                <span class="keywordtype">double</span> confidence;</div><div class="line">                <a name="a42"></a><a class="code" href="../../d2/de8/group__core__array.html#gab473bf2eb6d14ff97e89b355dac20707">minMaxLoc</a>(scores, 0, &amp;confidence, 0, &amp;classIdPoint);</div><div class="line">                <span class="keywordflow">if</span> (confidence &gt; confThreshold)</div><div class="line">                {</div><div class="line">                    <span class="keywordtype">int</span> centerX = (int)(data[0] * frame.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>);</div><div class="line">                    <span class="keywordtype">int</span> centerY = (int)(data[1] * frame.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>);</div><div class="line">                    <span class="keywordtype">int</span> width = (int)(data[2] * frame.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>);</div><div class="line">                    <span class="keywordtype">int</span> height = (int)(data[3] * frame.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>);</div><div class="line">                    <span class="keywordtype">int</span> left = centerX - width / 2;</div><div class="line">                    <span class="keywordtype">int</span> top = centerY - height / 2;</div><div class="line"></div><div class="line">                    classIds.push_back(classIdPoint.<a name="a43"></a><a class="code" href="../../db/d4e/classcv_1_1Point__.html#a4c96fa7bdbfe390be5ed356edb274ff3">x</a>);</div><div class="line">                    confidences.push_back((<span class="keywordtype">float</span>)confidence);</div><div class="line">                    boxes.push_back(<a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a>(left, top, width, height));</div><div class="line">                }</div><div class="line">            }</div><div class="line">        }</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">        <a class="code" href="../../db/de0/group__core__utils.html#ga5b48c333c777666e076bd7052799f891">CV_Error</a>(<a class="code" href="../../d1/d0d/namespacecv_1_1Error.html#a759fa1af92f7aa7377c76ffb142abccaab5d0063314f64d350a93d091b542d465">Error::StsNotImplemented</a>, <span class="stringliteral">&quot;Unknown output layer type: &quot;</span> + outLayerType);</div><div class="line"></div><div class="line">    <span class="comment">// NMS is used inside Region layer only on DNN_BACKEND_OPENCV for another backends we need NMS in sample</span></div><div class="line">    <span class="comment">// or NMS is required if number of outputs &gt; 1</span></div><div class="line">    <span class="keywordflow">if</span> (outLayers.size() &gt; 1 || (outLayerType == <span class="stringliteral">&quot;Region&quot;</span> &amp;&amp; backend != <a name="a44"></a><a class="code" href="../../d6/d0f/group__dnn.html#gga186f7d9bfacac8b0ff2e26e2eab02625a88591466239dde609ae676cec698a5b1">DNN_BACKEND_OPENCV</a>))</div><div class="line">    {</div><div class="line">        std::map&lt;int, std::vector&lt;size_t&gt; &gt; class2indices;</div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i &lt; classIds.size(); i++)</div><div class="line">        {</div><div class="line">            <span class="keywordflow">if</span> (confidences[i] &gt;= confThreshold)</div><div class="line">            {</div><div class="line">                class2indices[classIds[i]].push_back(i);</div><div class="line">            }</div><div class="line">        }</div><div class="line">        std::vector&lt;Rect&gt; nmsBoxes;</div><div class="line">        std::vector&lt;float&gt; nmsConfidences;</div><div class="line">        std::vector&lt;int&gt; nmsClassIds;</div><div class="line">        <span class="keywordflow">for</span> (std::map&lt;<span class="keywordtype">int</span>, std::vector&lt;size_t&gt; &gt;::iterator it = class2indices.begin(); it != class2indices.end(); ++it)</div><div class="line">        {</div><div class="line">            std::vector&lt;Rect&gt; localBoxes;</div><div class="line">            std::vector&lt;float&gt; localConfidences;</div><div class="line">            std::vector&lt;size_t&gt; classIndices = it-&gt;second;</div><div class="line">            <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i &lt; classIndices.size(); i++)</div><div class="line">            {</div><div class="line">                localBoxes.push_back(boxes[classIndices[i]]);</div><div class="line">                localConfidences.push_back(confidences[classIndices[i]]);</div><div class="line">            }</div><div class="line">            std::vector&lt;int&gt; nmsIndices;</div><div class="line">            <a name="a45"></a><a class="code" href="../../d6/d0f/group__dnn.html#ga9d118d70a1659af729d01b10233213ee">NMSBoxes</a>(localBoxes, localConfidences, confThreshold, nmsThreshold, nmsIndices);</div><div class="line">            <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i &lt; nmsIndices.size(); i++)</div><div class="line">            {</div><div class="line">                <span class="keywordtype">size_t</span> idx = nmsIndices[i];</div><div class="line">                nmsBoxes.push_back(localBoxes[idx]);</div><div class="line">                nmsConfidences.push_back(localConfidences[idx]);</div><div class="line">                nmsClassIds.push_back(it-&gt;first);</div><div class="line">            }</div><div class="line">        }</div><div class="line">        boxes = nmsBoxes;</div><div class="line">        classIds = nmsClassIds;</div><div class="line">        confidences = nmsConfidences;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> idx = 0; idx &lt; boxes.size(); ++idx)</div><div class="line">    {</div><div class="line">        <a name="_a46"></a><a class="code" href="../../d2/d44/classcv_1_1Rect__.html">Rect</a> box = boxes[idx];</div><div class="line">        drawPred(classIds[idx], confidences[idx], box.<a name="a47"></a><a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>, box.<a name="a48"></a><a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>,</div><div class="line">                 box.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a> + box.<a name="a49"></a><a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>, box.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a> + box.<a name="a50"></a><a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>, frame);</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> drawPred(<span class="keywordtype">int</span> classId, <span class="keywordtype">float</span> conf, <span class="keywordtype">int</span> left, <span class="keywordtype">int</span> top, <span class="keywordtype">int</span> right, <span class="keywordtype">int</span> bottom, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; frame)</div><div class="line">{</div><div class="line">    <a name="a51"></a><a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga07d2f74cadcf8e305e810ce8eed13bc9">rectangle</a>(frame, <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(left, top), <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(right, bottom), <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0, 255, 0));</div><div class="line"></div><div class="line">    std::string label = format(<span class="stringliteral">&quot;%.2f&quot;</span>, conf);</div><div class="line">    <span class="keywordflow">if</span> (!classes.empty())</div><div class="line">    {</div><div class="line">        <a class="code" href="../../db/de0/group__core__utils.html#gaf62bcd90f70e275191ab95136d85906b">CV_Assert</a>(classId &lt; (<span class="keywordtype">int</span>)classes.size());</div><div class="line">        label = classes[classId] + <span class="stringliteral">&quot;: &quot;</span> + label;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordtype">int</span> baseLine;</div><div class="line">    <a class="code" href="../../d6/d50/classcv_1_1Size__.html">Size</a> labelSize = <a name="a52"></a><a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga3d2abfcb995fd2db908c8288199dba82">getTextSize</a>(label, <a class="code" href="../../d6/d6e/group__imgproc__draw.html#gga0f9314ea6e35f99bb23f29567fc16e11afff8b973668df2e4028dddc5274310c9">FONT_HERSHEY_SIMPLEX</a>, 0.5, 1, &amp;baseLine);</div><div class="line"></div><div class="line">    top = <a name="a53"></a><a class="code" href="../../d7/dcc/group__core__utils__softfloat.html#ga78f988f6cfa6223610298cbd4f86ec66">max</a>(top, labelSize.<a class="code" href="../../d6/d50/classcv_1_1Size__.html#a1d289dce6b5d8006a54f3ee0259fc545">height</a>);</div><div class="line">    <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga07d2f74cadcf8e305e810ce8eed13bc9">rectangle</a>(frame, <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(left, top - labelSize.<a class="code" href="../../d6/d50/classcv_1_1Size__.html#a1d289dce6b5d8006a54f3ee0259fc545">height</a>),</div><div class="line">              <a class="code" href="../../db/d4e/classcv_1_1Point__.html">Point</a>(left + labelSize.<a class="code" href="../../d6/d50/classcv_1_1Size__.html#abfe0367b32c407ddccf5ddf92667c73d">width</a>, top + baseLine), <a name="a54"></a><a class="code" href="../../d1/da0/classcv_1_1Scalar__.html#ac1509a4b8454fe7fe29db069e13a2e6f">Scalar::all</a>(255), <a name="a55"></a><a class="code" href="../../d6/d6e/group__imgproc__draw.html#ggaf076ef45de481ac96e0ab3dc2c29a777a89c5f6beef080e6df347167f85e07b9e">FILLED</a>);</div><div class="line">    <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga5126f47f883d730f633d74f07456c576">putText</a>(frame, label, <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(left, top), <a class="code" href="../../d6/d6e/group__imgproc__draw.html#gga0f9314ea6e35f99bb23f29567fc16e11afff8b973668df2e4028dddc5274310c9">FONT_HERSHEY_SIMPLEX</a>, 0.5, <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>());</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> callback(<span class="keywordtype">int</span> pos, <span class="keywordtype">void</span>*)</div><div class="line">{</div><div class="line">    confThreshold = pos * 0.01f;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:29 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
